package com.dyz.leetcode._09_heapAndPriorityQueue;

import java.util.Arrays;
import java.util.PriorityQueue;

public class _215_findKthLargest {
    public int findKthLargest(int[] nums, int k) {
        Arrays.sort(nums);
        return nums[nums.length-k];

    }
    public int findKthLargest1(int[] nums, int k) {
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        for (int i = 0; i < k; i++) {
            queue.add(nums[i]);
        }

        for(int i = k; i<nums.length; i++){
            if(nums[i]>queue.peek()){
                queue.remove();
                queue.add(nums[i]);
            }
        }
        return queue.peek();
    }
}
